package com.xiyin.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiyin.entity.User1;
import com.xiyin.service.IUser1Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 功能点: 1.自动填充字段
 *        2.逻辑删除（当配置了全局逻辑删除后，所有的查询操作都会自动带上逻辑删除的条件。这意味着在执行查询时，系统会自动过滤掉那些已经被逻辑删除的记录）
 *        3.分页查询（底层通过初始化这两个属性，long current（当前页）, long size（每页展示条数））
 *        4.条件构造器lambda
 *          4.1.等于 (eq): eq(Column column, Object value)
 *              例子: lambda.eq(User::getId, 1L)
 *          4.2.不等于 (ne): ne(Column column, Object value)
 *              例子: lambda.ne(User::getId, 1L)
 *          4.3.大于 (gt): gt(Column column, Object value)
 *              例子: lambda.gt(User::getAge, 18)
 *          4.4.大于等于 (ge): ge(Column column, Object value)
 *              例子: lambda.ge(User::getAge, 18)
 *          4.5.小于 (lt): lt(Column column, Object value)
 *              例子: lambda.lt(User::getAge, 18)
 *          4.6.小于等于 (le): le(Column column, Object value)
 *              例子: lambda.le(User::getAge, 18)
 *          4.7.模糊匹配 (like): like(Column column, Object value)
 *              例子: lambda.like(User::getName, "张")
 *          4.8.不模糊匹配 (notLike): notLike(Column column, Object value)
 *              例子: lambda.notLike(User::getName, "张")
 *          4.9.包含 (in): in(Column column, Collection<?> coll)
 *              例子: lambda.in(User::getId, Arrays.asList(1L, 2L, 3L))
 *          4.10.不包含 (notIn): notIn(Column column, Collection<?> coll)
 *              例子: lambda.notIn(User::getId, Arrays.asList(1L, 2L, 3L))
 *          4.11.为空 (isNull): isNull(Column column)
 *              例子: lambda.isNull(User::getName)
 *          4.12.不为空 (isNotNull): isNotNull(Column column)
 *              例子: lambda.isNotNull(User::getName)
 *          4.13.排序 (orderByAsc): orderByAsc(SFunction<T, ?> column)
 *              例子: lambda.orderByAsc(User::getAge)
 *          4.14.排序 (orderByDesc): orderByDesc(SFunction<T, ?> column)
 *              例子: lambda.orderByDesc(User::getAge)
 *          4.15.分组 (groupBy): groupBy(SFunction<T, ?> column)
 *              例子: lambda.groupBy(User::getAge)
 *          4.16.条件 (and): and(Consumer<QueryWrapper<T>> consumer)
 *              例子: lambda.and(wrapper -> wrapper.eq(User::getAge, 18).like(User::getName, "张"))
 *          4.17.条件 (or): or(Consumer<QueryWrapper<T>> consumer)
 *              例子: lambda.or(wrapper -> wrapper.eq(User::getAge, 18).like(User::getName, "张"))
 * @author xukaihang
 * @since 2024-08-18
 */
@RestController
@RequestMapping("/user1")
public class User1Controller {
    @Autowired
    private IUser1Service user1Service;

    //新增
    @PostMapping("/add")
    public ResponseEntity<User1> save(@RequestBody User1 user1) {
        boolean save = user1Service.save(user1);
        return ResponseEntity.ok(save ? user1 : null);
    }
    //修改
    @PostMapping("/update")
    public ResponseEntity<User1> update(@RequestBody User1 user1) {
        boolean save = user1Service.updateById(user1);
        return ResponseEntity.ok(save ? user1 : null);
    }

    //删除
    @PostMapping("/delete/{id}")
    public ResponseEntity<Boolean> delete(@PathVariable("id") Long id) {
        return ResponseEntity.ok(user1Service.removeById(id));
    }
    //分页查询
    @GetMapping("/fenyechaxun")
    public Page<User1> findAll(
            @RequestParam(value = "page", defaultValue = "1") int currentPage,
            @RequestParam(value = "size", defaultValue = "10") int pageSize
    ) {
        //分页查询 构造器初始化
        Page<User1> page = new Page<>(currentPage, pageSize);
        user1Service.page(page, null);
        return page;
    }

    //条件构造器lambda
    @GetMapping("/lambda")
    public List<User1> lambdaQuery() {
        return user1Service.getUserByLambdaQueryWrapper();
    }

    //条件构造器lambda（复制查询，or）
    @GetMapping("/lambdaByOr")
    public List<User1> lambdaByOr() {
        return user1Service.getUserBylambdaByOrQueryWrapper();
    }
}
